package com.store.bookstore_backend.service.impl;

import com.store.bookstore_backend.domain.Book;
import com.store.bookstore_backend.domain.Shopcar;
import com.store.bookstore_backend.dto.ShopcarGetDTO;
import com.store.bookstore_backend.exception.BookStoreException;
import com.store.bookstore_backend.mapper.BookMapper;
import com.store.bookstore_backend.mapper.ShopcarMapper;
import com.store.bookstore_backend.service.ShopCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Service
public class ShopCarImpl implements ShopCarService {
    @Autowired
    ShopcarMapper shopcarMapper;
    @Autowired
    BookMapper bookMapper;

    @Override
    public List<ShopcarGetDTO> getShopCars(HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        List<Shopcar> shopCars = shopcarMapper.selectAllShopcar(userId);
        List<Book> books = new ArrayList<>();
        for (Shopcar shopcar : shopCars) {
            books.add(bookMapper.selectBookById(shopcar.getBookId()));
        }
        List<ShopcarGetDTO> shopcarGetDTOs = new ArrayList<>();
        for (int i = 0; i < shopCars.size(); i++) {
            ShopcarGetDTO shopcarGetDTO = new ShopcarGetDTO();
            shopcarGetDTO.setBookId(shopCars.get(i).getBookId());
            shopcarGetDTO.setId(shopCars.get(i).getId());
            shopcarGetDTO.setName(books.get(i).getName());
            shopcarGetDTO.setPrice(books.get(i).getPrice());
            shopcarGetDTO.setCover(books.get(i).getCover());
            shopcarGetDTO.setNum(shopCars.get(i).getNum());
            shopcarGetDTOs.add(shopcarGetDTO);
        }
        return shopcarGetDTOs;
    }

    @Override
    public void deleteShopCar(ArrayList<Integer> idList) {
        for (Integer id : idList) {
            shopcarMapper.deleteById(id);
        }
    }

    @Override
    public void updateShopCar(Integer id, HttpServletRequest request,Integer num) {
        Long userId = (Long) request.getAttribute("userId");
//        System.out.println(num);
        Book book = bookMapper.selectBookById(id);
        Shopcar shopcar = shopcarMapper.selectShopcarByUserId(userId, book.getId());
//        System.out.println(existed);
        if (shopcar == null) {
            shopcar = new Shopcar();
            shopcar.setBookId(id);
            shopcar.setUserId(userId);
            shopcar.setNum(num);
            shopcarMapper.insertShopcar(shopcar);
            return;
        }
        shopcarMapper.updateById(shopcar.getId(),num+shopcar.getNum());
    }

    @Override
    public void updateShopCarNum(Integer id, Integer num, HttpServletRequest request) {
        try {
            Long userId = (Long) request.getAttribute("userId");
            Shopcar shopcar = new Shopcar();
            shopcar.setBookId(id);
            shopcar.setUserId(userId);
            shopcar.setNum(num);
            shopcarMapper.updateShopcarnum(shopcar);
        } catch (Exception e) {
            throw new BookStoreException(BookStoreException.CodeMsgEnum.USER_TOKEN_IS_INVALID);
        }
    }
}

